<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Organization\Repository;

use App\Repository\IRepository;
use Hyperf\Database\Model\Builder;
use Plugin\Organization\Model\RdOrganizeArea;

/**
 * 工区表 Repository类
 */
class RdOrganizeAreaRepository extends IRepository
{
    public function __construct(
        protected readonly RdOrganizeArea $model
    )
    {
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 主键
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 所属项目线ID
        if (isset($params['proj_line_id']) && filled($params['proj_line_id'])) {
            $query->where('proj_line_id', '=', $params['proj_line_id']);
        }

        // 工区名称
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%' . $params['name'] . '%');
        }

        // 缩写
        if (isset($params['abbr']) && filled($params['abbr'])) {
            $query->where('abbr', 'like', '%' . $params['abbr'] . '%');
        }

        // 工区地址
        if (isset($params['addr']) && filled($params['addr'])) {
            $query->where('addr', 'like', '%' . $params['addr'] . '%');
        }

        // 经度
        if (isset($params['lat']) && filled($params['lat'])) {
            $query->where('lat', '=', $params['lat']);
        }

        // 纬度
        if (isset($params['lng']) && filled($params['lng'])) {
            $query->where('lng', '=', $params['lng']);
        }

        // 排序
        if (isset($params['sort']) && filled($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 工区编码
        if (isset($params['code']) && filled($params['code'])) {
            $query->where('code', 'like', '%' . $params['code'] . '%');
        }

        // 状态
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 软删除时间
        if (isset($params['deleted_at']) && filled($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [$params['deleted_at'][0], $params['deleted_at'][1]]
            );
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [$params['created_at'][0], $params['created_at'][1]]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [$params['updated_at'][0], $params['updated_at'][1]]
            );
        }

        // 备注
        if (isset($params['remark']) && filled($params['remark'])) {
            $query->where('remark', 'like', '%' . $params['remark'] . '%');
        }

        // 摘要
        if (isset($params['abstract']) && filled($params['abstract'])) {
            $query->where('abstract', 'like', '%' . $params['abstract'] . '%');
        }

        return $query;
    }

    public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
    {
        $result = $this->perQuery($this->getQuery(), $params)
            ->with('projLine:id,name')
            ->paginate(
                perPage: $pageSize,
                pageName: static::PER_PAGE_PARAM_NAME,
                page: $page,
            );
        return $this->handlePage($result);
    }

    protected function getOrderByParamName(): string
    {
        return 'sort';
    }
}